package de.g18.scavengerhunt.utils;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.util.Base64;
import android.util.Log;
import de.g18.scavengerhunt.AppApplicationContext;

/**
 * This class provides methods for processing pictures
 */
public class ImageUtils {

	private static final String LOG_TAG = ImageUtils.class.getSimpleName();

	public static String encodeImageBase64(Bitmap bitmap) {
		Bitmap toEncode = getResizedBitmap(bitmap, 100, 100);
		return Base64.encodeToString(getByteArrayFromBitmap(toEncode), Base64.DEFAULT);
	}

	public static Bitmap getBitmapFromBase64(String encodedImage) {
		byte[] decodedString = Base64.decode(encodedImage, Base64.DEFAULT);
		return BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
	}

	private static byte[] getByteArrayFromBitmap(Bitmap bitmap) {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
		return baos.toByteArray();
	}

	public static Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
		int origWidth = bm.getWidth();
		int origHeight = bm.getHeight();

		float scaleSize = 0;
		if (origWidth >= origHeight) {
			scaleSize = ((float) newWidth) / origWidth;
		} else {
			scaleSize = ((float) newHeight) / origHeight;
		}

		int scaledWidth = (int) (scaleSize * origWidth);
		int scaledHeight = (int) (scaleSize * origHeight);

		Log.d(LOG_TAG, "Resized image from " + origWidth + " x " + origHeight + " to " + scaledWidth + " x " + scaledHeight + " pixels");
		return Bitmap.createScaledBitmap(bm, scaledWidth, scaledHeight, false);
	}
	
	/**
	 * Gets a bitmap from an uri.
	 * 
	 * @param fileName
	 * @param url
	 * @return Bitmap
	 */
	public static Bitmap getBitmapFromUri(String fileName, Uri url) {
		File cacheDir;
		// If the device has an SD card
		if(android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {
			cacheDir = new File(android.os.Environment.getExternalStorageDirectory(), ".OCFL311");
		} else {
			// It does not have an SD card
			cacheDir = AppApplicationContext.getCurrentActivity().getCacheDir();
		}
		if(!cacheDir.exists()) {
			if(!cacheDir.mkdirs()) {
				Log.w(LOG_TAG, "Unable to create temporary folder for selected image to download");
			}
		}
		
		File f = new File(cacheDir, fileName);
		try {
			InputStream is = null;
			if(url.toString().startsWith("content://com.google.android.gallery3d")) {
				is = AppApplicationContext.getCurrentActivity().getContentResolver().openInputStream(url);
			} else {	
				is = new URL(url.toString()).openStream();			
			}
			OutputStream os = new FileOutputStream(f);
			GameUtils.copyStream(is, os);
			os.close();
			return BitmapFactory.decodeFile(f.getPath());
		} catch (Exception e) {
			Log.e(LOG_TAG, "Exception while getting bitmap from URI " + e.getMessage());
		}
		return null;
	}
}
